/**
 * @file  CreateBookModal
 * @author xiao_xie
 * @date 2025-09-17
 */

import { Form, Input, Modal } from "antd";
import TextArea from "antd/es/input/TextArea";
import React, { useImperativeHandle } from "react";
import {CoverUpload} from "./Coverupload";

interface Props {
  isOpen: boolean;
  handleClose: () => void;
  handleOk: () => void;
}

export interface CreateBook {
  name: string;
  author: string;
  description: string;
  cover: string;
}
const layout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 18 }
}

const CreateBookRef = React.forwardRef((props: Props, ref:any) =>{
    const [form] = Form.useForm();
    const { isOpen, handleClose, handleOk } = props;

    const handleFormValidate = async () => {
      try {
        await form.validateFields();
        const values = form.getFieldsValue() as CreateBook;
        form.resetFields();
        return values;
      } catch (error) {
        console.log('validate error', error)
        return false;
      }
    }
    useImperativeHandle(
      ref,
      () => {
        return {
          handleFormValidate
        }   
      },
      [],
    )

    return (<Modal
            title="添加图书"
            open={isOpen}
            onCancel={handleClose}
            onOk={handleOk}
            okText="确认"
            cancelText="取消"
          >
            <Form form={form} {...layout}>
              <Form.Item
                label="图书名称"
                name="name"
                rules={[{ required: true, message: "请输入图书名称" }]}
              >
                <Input placeholder="请输入图书名称" />
              </Form.Item>
              <Form.Item
                label="作者"
                name="author"
                rules={[{ required: true, message: "请输入图书作者" }]}
              >
                <Input placeholder="请输入图书作者" />
              </Form.Item>
              <Form.Item
                label="描述"
                name="description"
                rules={[{ required: true, message: "请输入图书描述!" }]}
              >
                <TextArea />
              </Form.Item>
              <Form.Item
                label="封面"
                name="cover"
                rules={[{ required: true, message: "请上传图书封面!" }]}
              >
                <CoverUpload />
              </Form.Item>
            </Form>
          </Modal>)
})

export default CreateBookRef;
